﻿using System;

#if WINDOWS_PHONE
namespace Fibre.WP7.Threading
#else
#if SILVERLIGHT
namespace Fibre.Silverlight.Threading
#else
namespace Fibre.Threading
#endif
#endif
{
    internal class WorkCompleteContext<T> : WorkCompleteContext, IWorkCompleteContext<T>
    {
        public WorkCompleteContext(AsyncWork asyncWork)
            : base(asyncWork)
        {
        }

        public T Result
        {
            get;
            set;
        }
    }

    internal class WorkCompleteContext : IWorkCompleteContext
    {
        private readonly AsyncWork _asyncWork;

        public WorkCompleteContext(AsyncWork asyncWork)
        {
            _asyncWork = asyncWork;
        }

        public Exception Error { get; internal set; }

        public IWorkContext AddWork(Action work)
        {
            return _asyncWork.AddWork(work);
        }

        public IWorkContext AddWork<TArg>(Func<TArg, AsyncCallback, object, IAsyncResult> work, TArg asyncArgument, Action<IAsyncResult> complete)
        {
            return _asyncWork.AddWork(work, asyncArgument, complete);
        }

        public IWorkContext<T> AddWork<T>(Func<T> work)
        {
            return _asyncWork.AddWork(work);            
        }

        public IWorkContext<T> AddWork<T>(Func<AsyncCallback, object, IAsyncResult> work, Func<IAsyncResult, T> complete)
        {
            return _asyncWork.AddWork(work, complete);
        }

        public IWorkContext<TResult> AddWork<TArg, TResult>(Func<TArg, AsyncCallback, object, IAsyncResult> work, TArg asyncArgument, Func<IAsyncResult, TResult> complete)
        {
            return _asyncWork.AddWork(work, asyncArgument, complete);
        }

        public IWorkContext AddWork<TArg, TArg2>(Func<TArg, TArg2, AsyncCallback, object, IAsyncResult> work, TArg asyncArgument, TArg2 asyncArgument2, Action<IAsyncResult> complete)
        {
            return _asyncWork.AddWork(work, asyncArgument, asyncArgument2, complete);
        }

        public IWorkContext<TResult> AddWork<TArg, TArg2, TResult>(
            Func<TArg, TArg2, AsyncCallback, object, IAsyncResult> work, 
            TArg asyncArgument, 
            TArg2 asyncArgument2, 
            Func<IAsyncResult, TResult> complete)
        {
            return _asyncWork.AddWork(work, asyncArgument, asyncArgument2, complete);
        }

        public IWorkContext<T> AddWork<T, TArg>(Func<TArg, AsyncCallback, object, IAsyncResult> work, TArg asyncArgument, Func<IAsyncResult, T> complete)
        {
            return _asyncWork.AddWork(work, asyncArgument, complete);
        }

        public IWorkContext<T> AddWork<T, TArg, TArg2>(Func<TArg, TArg2, AsyncCallback, object, IAsyncResult> work, TArg asyncArgument, TArg2 asyncArgument2, Func<IAsyncResult, T> complete)
        {
            return _asyncWork.AddWork(work, asyncArgument, asyncArgument2, complete);
        }

        public IWorkContext AddWork(Func<AsyncCallback, object, IAsyncResult> work, Action<IAsyncResult> complete)
        {
            return _asyncWork.AddWork(work, complete);
        }
    }
}